package com.virtualCat.dao;

import com.virtualCat.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface UserDao {

    @Select("select count(1) from t_user where openid=#{openid}")
    int selectUserByOpenid(@Param("openid")String openid);

    @Select("select openid,unionid,nickname,imgurl,sex,history,receiver,phone,area,build_number,cat_food,obtain from t_user where openid=#{openid}")
    @Results({
        @Result(column="build_number",property="build_number"),
            @Result(column = "cat_food",property = "cat_food")}
    )
    User findByOpenid(@Param("openid") String openid);

    @Insert("INSERT INTO t_user(openid,unionid,nickname,imgurl,sex,history,phone,area,build_number,cat_food,obtain) " +
            "VALUES(#{openid},#{unionid},#{nickName},#{imgUrl},#{sex},#{history},#{phone},#{area},#{build_number},#{cat_food},#{obtain})")
    int insertUser(User user);

    @Update("update t_user set nickname=#{nickName},imgurl=#{imgUrl},sex=#{sex},history=#{history} where openid=#{openid}")
    int updateInfo(User user);

    @Update("update t_user set receiver=#{receiver},phone=#{phone},area=#{area},build_number=#{build_number} where openid=#{openid}")
    int updateUser(User user);

    @Update("update t_user set history=#{history} where openid=#{openid}")
    int updateHistory(@Param("openid")String openid,@Param("history") int history);

    @Update("update t_user set obtain=#{loginTime} where openid=#{openid}")
    int updateLoginTime(@Param("loginTime")Date obtain,@Param("openid")String openid);

    @Update("update t_user set cat_food=#{catFood},history=#{history} where openid=#{openid}")
    int updateFoodAndHistory(@Param("catFood")Integer catFood,@Param("history")Integer history,@Param("openid")String openid);

    @Select("select cat_food from t_user where openid = #{openid}")
    int selectFoodByopenid(@Param("openid")String openid);

    @Update("update t_user set cat_food = #{catFood} where openid = #{openid}")
    int updateFoodByopenid(@Param("catFood")int catFood,@Param("openid")String openid);

    @Select("select * from t_user where openid IN (#{openids})")
    List<User> findByOpenIds(@Param("openids")String openids);

    @Select("select * from t_user")
    List<User> findAll();

    @Select("select count(*) from t_user")
    int countUser();

    @Delete("delete * from t_user where openid = #{openid}")
    int deleteUser(@Param("openid") String openid);

}
